package nc.bs.pub.action;


import java.util.Objects;

import com.microsoft.sqlserver.jdbc.StringUtils;

import nc.bs.common.projectFAUtils;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.itf.pu.m25.IInvoiceMaintain;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.pu.m25.entity.InvoiceItemVO;
import nc.vo.pu.m25.entity.InvoiceVO;
import nc.vo.pu.m25.env.InvoiceUIToBSEnv;
import nc.vo.pu.m25.pub.InvoiceEnvExtractUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.uap.pf.PFBusinessException;

public class N_25_SAVEBASE extends AbstractCompiler2 {
	
	//begin 231020 lhw
	private BaseDAO baseDao;
	
	private BaseDAO getBD() {
		if(baseDao == null) {
			baseDao = new BaseDAO();
		}
		return baseDao;
	}
	//end
	
    public N_25_SAVEBASE() {
    }

    public String getCodeRemark() {
        return "  /*************从平台取得由该动作传入的入口参数。本方法取得需要的VO。************/\n nc.vo.pu.m25.entity.InvoiceVO[] inObject  =(nc.vo.pu.m25.entity.InvoiceVO[]) getVos ();\n /**************设置参数******************************************************/\n  setParameter(\"INVO\", inObject);\n /**************执行采购发票的保存（更新）方法 ********************************/\n  return (nc.vo.pu.m25.entity.InvoiceVO[])runClass(\"nc.impl.pu.m25.action.InvoiceMaintainImpl\",\"update\",\n        \"&INVO:nc.vo.pu.m25.entity.InvoiceVO\",vo,m_keyHas,m_methodReturnHas);\n";
    }

    public Object runComClass(PfParameterVO vo) throws BusinessException {
        try {
            super.m_tmpVo = vo;
            Object retObj = null;
            InvoiceVO[] vos = (InvoiceVO[])((InvoiceVO[])this.getVos());
            InvoiceUIToBSEnv[] envs = InvoiceEnvExtractUtil.getEnvs(vo);
            
            
            //lhw 231128 保存前校验项目 begin
			new projectFAUtils().projectDataApprove("po_invoice", vos, "save");
			//end
            
            //begin 231020 lhw 采购合同类型 是否是正式合同
            //发票归属
            int finvoicetype = vos[0].getParentVO().getFinvoicetype();
            if(finvoicetype == 0) {//采购发票
            	InvoiceItemVO[] itemvos = vos[0].getChildrenVO();
            	boolean f = false;
            	
            	for (InvoiceItemVO ivo : itemvos) {
            		String pk_ordersql = "select pk_order from po_order where vbillcode = '"+ivo.getVfirstcode()+"' and dr = 0";
            		String pk_order = (String) getBD().executeQuery(pk_ordersql, new ColumnProcessor());
            		if(StringUtils.isEmpty(pk_order)) {
            			continue;
            		}
            		String ccontractidsql = "select ccontractid from po_order_b where pk_order = '"+pk_order+"' and dr = 0";
            		String ccontractid = (String) getBD().executeQuery(ccontractidsql, new ColumnProcessor());
            		if(StringUtils.isEmpty(ccontractid)) {
            			continue;
            		}
            		String vdefsql = "select vdef8 from ct_pu where pk_ct_pu = '"+ccontractid+"' and dr = 0";
            		String vdef1 = (String) getBD().executeQuery(vdefsql, new ColumnProcessor());
            		if(!Objects.equals(vdef1, "Y")) {
            			f = true;
            			break;
            		}
            		
				}
            	if(f == true) {
            		throw new RuntimeException("该业务合同非正式合同，需变更为正式合同才允许保存");
            	}
            }
            //end 20231025 lhw

            retObj = ((IInvoiceMaintain)NCLocator.getInstance().lookup(IInvoiceMaintain.class)).save(vos, envs);
            return retObj;
        } catch (Exception var5) {
            if (var5 instanceof BusinessException) {
                throw (BusinessException)var5;
            } else {
                throw new PFBusinessException(var5.getMessage(), var5);
            }
        }
    }
   
}
